<template>
  <div class="home">
    <el-container class="wrapper-home">
      <el-header>
        <img src="../../assets/heima.png" alt />
        <h1>电商后台管理系统</h1>
        <el-button class="logout-btn" type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' :'200px'">
          <div class="aside-toggle" @click="toggleAside">|||</div>
          <div>
            <el-menu
              class="el-menu-vertical-demo"
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409EFF"
              :collapse="isCollapse"
              :collapse-transition="false"
              :unique-opened="true"
              router
              :default-active="$route.path"
            >
              <!-- :default-active="$route.path" -->
              <el-submenu :index="menu.id+''" v-for="(menu,index) in menuList" :key="index">
                <template slot="title">
                  <i class="iconfont" :class="iconList[index]"></i>
                  <span class="menu-item">{{menu.authName}}</span>
                </template>
                <el-menu-item
                  :index="'/'+child.path+''"
                  v-for="(child,index) in menu.children"
                  :key="index"
                >
                  <!-- @click="setActive('/'+child.path+'')" -->
                  <i class="el-icon-menu"></i>
                  <span>{{child.authName}}</span>
                  <!-- 和nav-menu的router属性不能同时使用 <router-link tag="span" :to="'/'+child.path+''">{{child.authName}}</router-link> -->
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </div>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  props: {
    index: String
  },
  data() {
    return {
      isCollapse: false,
      // 左侧菜单
      menuList: [],
      iconList: ['icon-geren4', 'icon-quanxianfenji', 'icon-shangpingouwudai2', 'icon-dingdanhui', 'icon-shuju']
      // 被激活的地址
      // activePath: ''
    }
  },
  created() {
    this.getMenus()
    // this.activePath = window.sessionStorage.getItem('path')
  },
  methods: {
    toggleAside() {
      this.isCollapse = this.isCollapse ? false : true
    },
    logout() {
      const token = window.sessionStorage.getItem('token')
      if (token) {
        // window.sessionStorage.removeItem('token')
        window.sessionStorage.clear()
        this.$router.push('/login')
      }
    },
    // setActive(val) {
    //   window.sessionStorage.setItem('path', val)
    //   // 更新地址
    //   this.activePath = val
    // },
    // 获取左侧菜单
    async getMenus() {
      const { data: res } = await this.$axios.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    }
  },
  // watch: {
  //   '$route.path': function(newval) {
  //     this.activePath = newval
  //   }
  // },
  mounted() {}
}
</script>

<style lang='less' scoped>
.home,
.wrapper-home {
  height: 100%;
}
.el-header {
  background: #373d41;
  h1,
  img {
    float: left;
  }
  img {
    vertical-align: middle;
  }
  h1 {
    font-size: 24px;
    color: #fff;
    font-weight: normal;
    line-height: 60px;
    margin: 0 10px;
  }
  .logout-btn {
    float: right;
    margin-top: 10px;
  }
}
.el-aside {
  //   width: 201px;
  background: #333744;
}
.aside-menu {
  font-size: 20px;
}
.menu-item {
  font-size: 20px;
}
.aside-toggle {
  height: 24px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  background: #4a5064;
  width: 100%;
  cursor: pointer;
}
.el-submenu .el-menu-item {
  height: 60px !important;
  line-height: 60px !important;
}
.el-menu {
  border-right: 0;
}
.collapse {
  width: 200px;
}
.el-main {
  background: #eaedf1;
}
</style>
